package org.jeecg.modules.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

@Slf4j
public class HttpUtil {


    /**
     * 发起批量转账API 批量转账到零钱
     *
     * @param requestUrl
     * @param requestJson 组合参数
     * @param wechatPayserialNo 商户证书序列号
     * @param mchID4M  商户号
     * @param privatekeypath  商户私钥证书路径
     * @return
     */
//    public static String postTransBatRequest(
//            String requestUrl,
//            String requestJson,
//            String platform_wechatPayserialNo,
//            String wechatPayserialNo,
//            String mchID4M,
//            String privatekeypath) {
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//        CloseableHttpResponse response = null;
//        HttpEntity entity = null;
//        try {
//            //商户私钥证书
//            HttpPost httpPost = new HttpPost(requestUrl);
//            // NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore，不能正确的设置字符集，可能导致签名错误
//            httpPost.addHeader("Content-Type", "application/json");
//            httpPost.addHeader("Accept", "application/json");
//            //httpPost.addHeader("Wechatpay-Serial", wechatPayserialNo);
//            httpPost.addHeader("Wechatpay-Serial", platform_wechatPayserialNo);//用了隐私信息加密时，上传的微信支付平台公钥的序列号
//            //-------------------------核心认证 start-----------------------------------------------------------------
//
//            String strToken = VechatPayV3Util.getToken("POST",
//                    "/v3/transfer/batches",
//                    requestJson,mchID4M,wechatPayserialNo, privatekeypath);
//
//            System.out.println("微信转账token "+strToken);
//            // 添加认证信息
//            httpPost.addHeader("Authorization",
//                    "WECHATPAY2-SHA256-RSA2048" + " "
//                            + strToken);
//            //---------------------------核心认证 end---------------------------------------------------------------
//            httpPost.setEntity(new StringEntity(requestJson, "UTF-8"));
//            //发起转账请求
//            response = httpclient.execute(httpPost);
//            entity = response.getEntity();//获取返回的数据
//            return EntityUtils.toString(entity);
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//            e.printStackTrace();
//        } finally {
//            // 关闭流
//        }
//        return null;
//    }


    /**
     * 发送HTTP_GET请求
     *
     * @param reqURL
     *            请求地址(含参数)
     * @param decodeCharset
     *            解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码
     * @return 远程主机响应正文
     */
    public static String sendGetRequest(String reqURL,String auth,String decodeCharset) {
        long responseLength = 0; // 响应长度
        String responseContent = null; // 响应内容
        HttpClient httpClient = new DefaultHttpClient(); // 创建默认的httpClient实例

        HttpGet httpGet = new HttpGet(reqURL); // 创建org.apache.http.client.methods.HttpGet
        httpGet.addHeader("Authorization", auth);
        httpGet.addHeader("Accept", "application/json");
        httpGet.addHeader("User-Agent", "https://zh.wikipedia.org/wiki/User_agent");
        try {
            HttpResponse response = httpClient.execute(httpGet); // 执行GET请求
            HttpEntity entity = response.getEntity(); // 获取响应实体
            if (null != entity) {
                responseLength = entity.getContentLength();
                responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
                EntityUtils.consume(entity); // Consume response content
            }
        } catch (ClientProtocolException e) {
            System.out.println("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下");
        } catch (IOException e) {
            System.out.println("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下");
        } finally {
            httpClient.getConnectionManager().shutdown(); // 关闭连接,释放资源
        }
        return responseContent;
    }

}
